package com.googlecode.jmicroerp.service;

import com.googlecode.jmicroerp.repository.ZZZRepository;
import java.math.BigDecimal;

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.util.GregorianCalendar;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Alexandru.Codescu
 */
@Service(value = "zzzService")
public class ZZZService {
    @Autowired
    private ZZZRepository zzzRepository;
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
                 
        return cal.getTime();
    }
    
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -7);
        
        return calendar.getTime();
    }
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
    
    public List<Object[]> queryZZZByMagazinAll(Date dayStart, Date dayEnd, Long idMagazin) {
        Date dayS = getTodayDate();
        Date dayE = addDays(dayS, 1);
        return zzzRepository.queryZZZByMagazinAll(dayStart, dayEnd, idMagazin);
    }
    
    public BigDecimal queryZZZByMagazinSum(Date dayStart, Date dayEnd, Long idMagazin) {
        List<BigDecimal> vanzari = zzzRepository.queryZZZByMagazinSum(dayStart, dayEnd, idMagazin);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }

}